package algorithm.swordoff;


/**
 * 对称的二叉树
 *
 * 最小子结构,其中A和B的子树可以为空,此时A和B是叶子节点
 *      A         B
 *     / \       / \
 *   A.L A.R   B.L B.R
 */

public class SQ28 {

    public boolean isSymmetric(TreeNode root) {
        return root == null || sameValueAndMirrorSon(root.left, root.right);
    }

    public boolean sameValueAndMirrorSon(TreeNode A, TreeNode B) {
        if (A == null && B == null) return true;
        else if (A == null || B == null) return false;

        return A.val == B.val && sameValueAndMirrorSon(A.left, B.right) && sameValueAndMirrorSon(A.right, B.left);

    }

}
